﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Grid;
using Control;
using LPV_POS.Module;
using DevExpress.XtraGrid.Views.Base;

namespace LPV_POS.mh_pos
{
    public partial class mh_doidvt : DevExpress.XtraEditors.XtraForm
    {
        thuvien tv;
        DataTable tb;
        
        string[] s;
        object[] o;
        #region  cac ham va phuong thuc
        //  danh sach hang hoa
        private DataTable f_dshanghoa()
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose" };
            o = new object[] { 1, 1 };
            tb = tv.SP_SelectDataTable("sp_mh_chuyendoidonvi", s, o);
            return tb;
        }
        //  danh sach don vi tinh
        private DataTable f_dsdonvitinh()
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose" };
            o = new object[] { 1, 2 };
            tb = tv.SP_SelectDataTable("sp_mh_chuyendoidonvi", s, o);
            DataRow myNewRow;
            myNewRow = tb.NewRow();
            myNewRow["OBJID"] = "1";
            myNewRow["OBJNAME"] = "--- Chọn ĐVT ---";
            tb.Rows.Add(myNewRow);
          //  tb.Rows.Add(n);
             tb.Columns.Add("STT", typeof(int));
            for (int i = 0; i < tb.Rows.Count; i++)
                tb.Rows[i]["STT"] = i + 1;

            DataView dv_temp = tb.DefaultView;
            dv_temp.Sort = "STT DESC";
            return dv_temp.ToTable();
        }

        // hien thi danh sach hang hoa qui doi don vi tinh
        private DataTable f_dshhquidoidvt(string mahanghoa)
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose", "@mahanghoa" };
            o = new object[] { 1, 3, mahanghoa };
            tb = tv.SP_SelectDataTable("sp_mh_chuyendoidonvi", s, o);
         
            DataView dv_temp = tb.DefaultView;
       
            return dv_temp.ToTable();
        }

        // Them một hang hoa chuyen doi don vi tinh
        private int f_inserthhdoidvt(string mahh, string dvt_first, decimal sl_first, decimal dongia, string dvt_last, decimal sl_last)
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose","@MAHH","@DVT_FIRST","@SL_FIRST","@DONGIA","@DVT_LAST","@SL_LAST" };
            o = new object[] { 2, 1,mahh,dvt_first,sl_first,dongia,dvt_last,sl_last};
            int ketqua = tv.SP_Update("sp_mh_chuyendoidonvi", s, o);
            return ketqua;
        }

        // Sua mot hang hoa chuyen doi don vi
        private int f_updatehhdoidvt(int stt,string mahh,string dvt_first, decimal sl_first, decimal dongia, string dvt_last, decimal sl_last)
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose","@STT", "@MAHH", "@DVT_FIRST", "@SL_FIRST", "@DONGIA", "@DVT_LAST", "@SL_LAST" };
            o = new object[] { 3, 1,stt,mahh, dvt_first, sl_first, dongia, dvt_last, sl_last };
            int ketqua = tv.SP_Update("sp_mh_chuyendoidonvi", s, o);
            return ketqua;
        }
        // delete hang hoa qui doi
        private int f_detelehhdoidvt(string mahh, string dvt_first, string dvt_last)
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose", "@MAHH", "@DVT_FIRST",  "@DVT_LAST"};
            o = new object[] { 4, 1, mahh, dvt_first,dvt_last };
            int ketqua = tv.SP_Update("sp_mh_chuyendoidonvi", s, o);
            return ketqua;
        }

        // Tim hang hoa khi nhan nut tim
        private DataTable f_selectfindhh(string mahhh, string tenhh,string tendvt)
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose", "@MAHH", "@TENHH", "@DVT_FIRST" };
            o = new object[] { 1, 4, mahhh,tenhh,tendvt };
            tb = tv.SP_SelectDataTable("sp_mh_chuyendoidonvi", s, o);
            return tb;
        }
        // 
        private DataTable f_findhh()
        {
                    DataTable tb;
                  string mahh;
                  string tenhh;
                  string tendvt;
              if (txt_mahanghoa.Text != "")
                  mahh = "'%" + txt_mahanghoa.Text + "%'";
              else
                  mahh = txt_mahanghoa.Text;

                if(txt_tenhanghoa.Text !="")     
                    tenhh = "'%" + txt_tenhanghoa.Text + "%'";
                else
                    tenhh = txt_tenhanghoa.Text;

                if(txt_donvitinh.Text !="")
                    tendvt = "'%" + txt_donvitinh.Text  +"%'";
                else
                    tendvt = txt_donvitinh.Text;

                tb = f_selectfindhh(mahh, tenhh, tendvt);
                return tb;

        }
        // hien thi len combobox
        private void f_showdataoncombobox(AXCombobox cb, DataTable tb, string value, string display)
        {
            cb.Datasource = tb;
            cb.DisplayField = display;
            cb.ValueFiled = value;
            cb._Init();
        }

        #endregion 
        public mh_doidvt()
        {
            InitializeComponent();
            f_showdataoncombobox(com_dvt, f_dsdonvitinh(), "OBJID", "OBJNAME");
            f_showdataoncombobox(com_quydoidvt, f_dsdonvitinh(), "OBJID", "OBJNAME");
           
        }    

        #region cac su kien

                private void mh_doidvt_Load(object sender, EventArgs e)
                {
                    // hien thi ds hang hoa len gird_hanghoa
                    luoi_hanghoa.DataSource = f_dshanghoa();
                    // hien thi data len combobox
      
                    //dua gia tri len
                    //com_dvt._SetselectedID("1");
                    //com_quydoidvt._SetselectedID("1");
                    //get : lay gia tri ve
                    bton_dongia.Enabled = false;
                    bton_slquydoi.Enabled = false;
                    bton_slcanquydoi.Enabled = false;
                    bton_luu.Enabled = false;
                  //  string a = com_dvt._GetselectedID();
            
                }

                private void grid_hanghoa_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
                {
                    GridView grid = (GridView)sender;
                    //   str_makho = grid.GetRowCellValue(grid_khohang.FocusedRowHandle, col_makho).ToString();
                    int chiso = e.FocusedRowHandle;
                    string _chiso = Convert.ToString(chiso);
                    GridColumn col = grid_hanghoa.FocusedColumn;
                    txt_tenhh_2.Text = grid_hanghoa.GetRowCellDisplayText(chiso, col_tenhanghoa) + "";
                    txt_tenhh_2.Text = grid_hanghoa.GetRowCellDisplayText(chiso, col_tenhanghoa) + "";
                    com_dvt.Text = grid_hanghoa.GetRowCellDisplayText(chiso, col_donvitinh) + "";

                    // hien thi danh sach hang hoa thay doi don vi tinh      col_donvitinh
                    string mahh = grid_hanghoa.GetRowCellDisplayText(chiso, col_mahanghoa) + "";
                    luoi_hanghoaquydoi.MainView = grid_hhquidoi;
                    luoi_hanghoaquydoi.DataSource = f_dshhquidoidvt(mahh);

                }

                private void bton_slcanquydoi_Click(object sender, EventArgs e)
                            {
                                //mh_so frm_so = new mh_so();
                                //frm_so.ControlBox = false;
                                //Variable.str_position_frmso = "mh_doidvt_bton_slcanquydoi";
                                //frm_so.ShowDialog();
                                //if (Variable.str_so=="")
                                //    txt_slquydoi.Text = "1";
                                //else
                                //txt_slcanquydoi.Text = Variable.str_so;

                                string str_so = new cpublic().loadformso_point(500, 200);
                                if (str_so == "")
                                    txt_slquydoi.Text = "1";
                                else
                                    txt_slcanquydoi.Text = str_so;
                            }

               private void bton_slquydoi_Click(object sender, EventArgs e)
                            {
                                //mh_so frm_so = new mh_so();
                                //frm_so.ControlBox = false;
                                //Variable.str_position_frmso = "mh_doidvt_bton_slquydoi";
                                //frm_so.ShowDialog();
                                //if (Variable.str_so == "")
                                //    txt_slcanquydoi.Text = "1";
                                //else
                                //txt_slquydoi.Text = Variable.str_so;

                                string str_so = new cpublic().loadformso_point(500, 200);
                                if (str_so == "")
                                    txt_slcanquydoi.Text = "1";
                                else
                                    txt_slquydoi.Text = str_so;
                            }
        #endregion

               private void bton_them_Click(object sender, EventArgs e)
               {

                 //  com_dvt._SetselectedID("1");
                   com_quydoidvt._SetselectedID("1");
                   txt_dongia.Text = "";
                   txt_slcanquydoi.Text = "1";
                   txt_slquydoi.Text = "1";
                   bton_dongia.Enabled = true;
                   bton_slquydoi.Enabled = true;
                   bton_slcanquydoi.Enabled = true;
                   bton_luu.Enabled = true;

                   Variable.str_trangthaithemsua = "them";

               }

               private void bton_sua_Click(object sender, EventArgs e)
               {
                   bton_dongia.Enabled = true;
                   bton_slquydoi.Enabled = true;
                   bton_slcanquydoi.Enabled = true;
                   bton_luu.Enabled = true;
                   Variable.str_trangthaithemsua = "sua";
               }

               private void bton_luu_Click(object sender, EventArgs e)
               {
                   int ketqua;
                   string id_dvt = com_dvt._GetselectedID();
                   if (id_dvt == "")
                   {
                       MessageBox.Show("Hàng hóa "+txt_tenhh_2.Text.ToUpper().Trim() + " chưa có ĐVT","ĐVT");
                       return;
                   }
                   string id_dvtquidoi = com_quydoidvt._GetselectedID();
                   if (id_dvt == "1" || id_dvtquidoi == "1")
                   {
                       Mod_messagebox mess = new Mod_messagebox();
                       mess.messagebox_pos("Thông báo", "Chưa chọn đơn vị tính", loaithongbao.canhbao.GetHashCode());
                       return;
                   }
                   if (txt_dongia.Text  == "")
                   {
                       Mod_messagebox mess = new Mod_messagebox();
                       mess.messagebox_pos("Thông báo", "Chưa nhập giá tiền", loaithongbao.canhbao.GetHashCode());
                       return;
                   }
                   
                        string mahh = grid_hanghoa.GetRowCellValue(grid_hanghoa.FocusedRowHandle, col_mahanghoa).ToString();
                       string dvt_first = com_dvt._GetselectedID();
                       decimal sl_first =Convert.ToDecimal( txt_slcanquydoi.Text);
                       decimal dongia = Convert.ToDecimal(txt_dongia.Text);
                        string dvt_last = com_quydoidvt._GetselectedID();
                        decimal sl_last = Convert.ToDecimal( txt_slquydoi.Text) ;
                        thuvien othuvien = new thuvien();
                        string sSQL = "";
                        if (Variable.str_trangthaithemsua == "them")
                        {
                            sSQL = "SELECT 1 FROM DM_DOIDVT WHERE MAHH = '" + mahh + "' AND DVT_FIRST = '" + dvt_first + "' AND DVT_LAST = '" + dvt_last + "'";
                            DataTable tbDM_DOIDVT = othuvien.SP_SelectDataTable("SP_EXECUTESQL", new string[] { "@sSQL" }, new string[] { sSQL });
                            if (tbDM_DOIDVT.Rows.Count > 0)
                            {
                                MessageBox_LPV.Show("Trùng dữ liệu \n Thêm mới không thành công.");
                                return;
                            }
                            ketqua = f_inserthhdoidvt(mahh, dvt_first, sl_first, dongia, dvt_last, sl_last);

                            luoi_hanghoaquydoi.MainView = grid_hhquidoi;
                            luoi_hanghoaquydoi.DataSource = f_dshhquidoidvt(mahh);

                            if (ketqua == -1)
                            {
                                Mod_messagebox mess = new Mod_messagebox();
                                mess.messagebox_pos("Thông báo", "Thêm không thành công", loaithongbao.canhbao.GetHashCode());
                                return;
                            }
                        }
                        else if (Variable.str_trangthaithemsua == "sua")
                        {
                            int stt = int.Parse(grid_hhquidoi.GetRowCellValue(grid_hhquidoi.FocusedRowHandle, col_stt).ToString());

                            sSQL = "SELECT 1 FROM DM_DOIDVT WHERE MAHH = '" + mahh + "' AND DVT_FIRST = '" + dvt_first + "' AND DVT_LAST = '" + dvt_last + "' AND STT <> " + stt;
                            DataTable tbDM_DOIDVT = othuvien.SP_SelectDataTable("SP_EXECUTESQL", new string[] { "@sSQL" }, new string[] { sSQL });
                            if (tbDM_DOIDVT.Rows.Count > 0)
                            {
                                MessageBox_LPV.Show("Trùng dữ liệu\n cập nhập không thành công.");
                                return;
                            }

                            ketqua = f_updatehhdoidvt(stt, mahh, dvt_first, sl_first, dongia, dvt_last, sl_last);

                            luoi_hanghoaquydoi.MainView = grid_hhquidoi;
                            luoi_hanghoaquydoi.DataSource = f_dshhquidoidvt(mahh);

                            if (ketqua == -1)
                            {
                                Mod_messagebox mess = new Mod_messagebox();
                                mess.messagebox_pos("Thông báo", "Chỉnh sửa không thành công", loaithongbao.canhbao.GetHashCode());
                                return;
                            }
                        }
               }

               private void grid_hhquidoi_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
               {
                   GridView grid = grid_hhquidoi;
                   //   str_makho = grid.GetRowCellValue(grid_khohang.FocusedRowHandle, col_makho).ToString();
                   int chiso = grid_hhquidoi.FocusedRowHandle;
                   string _chiso = Convert.ToString(chiso);
                   GridColumn col = grid_hhquidoi.FocusedColumn;
                   txt_dongia.Text = grid_hhquidoi.GetRowCellDisplayText(chiso, col_dongia) + "";
    //f_showdataoncombobox(com_dvt, f_dsdonvitinh(), "OBJID", "OBJNAME");
                   //f_showdataoncombobox(com_quydoidvt, f_dsdonvitinh(), "OBJID", "OBJNAME");
                   string madvt = grid_hhquidoi.GetRowCellDisplayText(chiso, col_dvtfist);
                   com_dvt._SetselectedID(madvt);
                   string madvtquidoi = grid_hhquidoi.GetRowCellDisplayText(chiso, col_dvtlast);
                   com_quydoidvt._SetselectedID(madvtquidoi);
                   string slcanquidoi = grid_hhquidoi.GetRowCellDisplayText(chiso, col_slcanquidoi);
                   txt_slcanquydoi.Text = slcanquidoi;
                   string slquidoi = grid_hhquidoi.GetRowCellDisplayText(chiso, col_slquidoi);
                   txt_slquydoi.Text = slquidoi;

                   bton_dongia.Enabled = false;
                   bton_slquydoi.Enabled = false;
                   bton_slcanquydoi.Enabled = false;
                  
               }

               private void bton_thoat_Click(object sender, EventArgs e)
               {
                   this.Close();
               }

               private void bton_xoa_Click(object sender, EventArgs e)
               {
                   int ketqua;
                   if (luoi_hanghoaquydoi.DefaultView.RowCount <= 0)    // neu khong co dong nao thi khong xoa duoc
                       return;
               
                   string mahhquidoi = grid_hhquidoi.GetRowCellValue(grid_hhquidoi.FocusedRowHandle, col_mahhquidoi).ToString();
                   string madvtcanquidoi = grid_hhquidoi.GetRowCellValue(grid_hhquidoi.FocusedRowHandle, col_dvtfist).ToString();
                   string madvtquidoi = grid_hhquidoi.GetRowCellValue(grid_hhquidoi.FocusedRowHandle, col_dvtlast).ToString();
                   Mod_messagebox mess = new Mod_messagebox();
                   mess.messagebox_pos("Thông báo", "Bạn muốn xóa?", loaithongbao.yesno.GetHashCode());
                   if (Variable.thongbao == true)
                   {
                       ketqua = f_detelehhdoidvt(mahhquidoi, madvtcanquidoi, madvtquidoi);
                       if (ketqua == 2)
                       {
                           //Mod_messagebox mess = new Mod_messagebox();
                           mess.messagebox_pos("Thông báo", "Xóa không thành công", loaithongbao.canhbao.GetHashCode());
                           return;
                       }
                       // xoa mot dong trong luoi ly do thu
                       int chiso;
                       chiso = ((ColumnView)luoi_hanghoaquydoi.Views[0]).FocusedRowHandle;
                       DataView tam = (DataView)luoi_hanghoaquydoi.Views[0].DataSource;
                       tam.Delete(chiso);
                   }
               }

               private void bton_tim_Click(object sender, EventArgs e)
               {
                   luoi_hanghoa.DataSource = f_findhh();
                   if (luoi_hanghoa.DefaultView.RowCount > 0)
                   {
                       // hien thi danh sach hang hoa thay doi don vi tinh
                       txt_tenhh_2.Text = grid_hanghoa.GetRowCellValue(grid_hanghoa.FocusedRowHandle, col_tenhanghoa).ToString();
                       string mahh = grid_hanghoa.GetRowCellValue(grid_hanghoa.FocusedRowHandle, col_mahanghoa).ToString();
                       luoi_hanghoaquydoi.MainView = grid_hhquidoi;
                       luoi_hanghoaquydoi.DataSource = f_dshhquidoidvt(mahh);
                   }

               }

               private void bton_timmahh_Click(object sender, EventArgs e)
               {
                   //mh_chu frm_chu = new mh_chu();
                   //frm_chu.ControlBox = false;

                   //Variable.str_position_frmchu = "bton_timmahh";
                   //frm_chu.ShowDialog();
                   //txt_mahanghoa.Text = Variable.str_chu;
                   txt_mahanghoa.Text = new cpublic().loadformchu_point(200, 140);
               }

               private void bton_timtenhh_Click(object sender, EventArgs e)
               {
                   //mh_chu frm_chu = new mh_chu();
                   //frm_chu.ControlBox = false;

                   //Variable.str_position_frmchu = "bton_timmahh";
                   //frm_chu.ShowDialog();
                   //txt_tenhanghoa.Text = Variable.str_chu;

                   txt_tenhanghoa.Text = new cpublic().loadformchu_point(200, 140);
               }

               private void bton_timdvt_Click(object sender, EventArgs e)
               {
                   //mh_chu frm_chu = new mh_chu();
                   //frm_chu.ControlBox = false;

                   //Variable.str_position_frmchu = "bton_timmahh";
                   //frm_chu.ShowDialog();
                   //txt_donvitinh.Text = Variable.str_chu;
                   txt_donvitinh.Text = new cpublic().loadformchu_point(200, 140);
               }

               private void bton_dongia_Click(object sender, EventArgs e)
               {
                   //mh_so frm_so = new mh_so();
                   //frm_so.ControlBox = false;
                   //Variable.str_position_frmso = "frm_doidvt_bton_dongia";
                   //frm_so.ShowDialog();
                   //if (Variable.str_so == "")
                   //    txt_dongia.Text = "1";
                   //else
                   //txt_dongia.Text = Variable.str_so;

                   string str_so = new cpublic().loadformso_point(500, 200);
                   if (str_so == "")
                       txt_dongia.Text = "1";
                   else
                       txt_dongia.Text = str_so;
               }

               private void txt_mahanghoa_KeyDown(object sender, KeyEventArgs e)
               {
                   if (e.KeyCode == Keys.Enter)
                       this.bton_tim_Click(null, null);
               }

               private void grid_hhquidoi_Click(object sender, EventArgs e)
               {
                   if (grid_hhquidoi.RowCount == 1)
                       this.grid_hhquidoi_FocusedRowChanged(null, null);
               }

             
             
             
       
    }
}